<?php
include('conect.php');

//connection String
$con = mysql_connect($hostname, $username, $password) or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $con);

mysql_query("SET NAMES utf8");
$acction = $_GET['acction'];
$arr = array();

if ($_GET['storeTip'] === 'grid') {
    $period = "";
    if ($acction === 'dateout') {
        $tip = "";
    } else if($acction === 'pro'){
        $tip = "AND d.pro='".$_GET["pro"]."'";
    } else if($acction === 'categ'){
        $tip = "AND c.subpro='".$_GET["subpro"]."'";
    } else if($acction === 'cild'){
        $tip = "AND d.cild='".$_GET["cild"]."'";
    }
    if($_GET['period'] === 'day'){
        $period = "AND DATE(d.data)=DATE(NOW())";
    } else if($_GET['period'] === 'year'){
        $period = "AND YEAR(d.data)=YEAR(CURRENT_DATE)";
    } else if($_GET['period'] === 'week'){
        $w = date("W");
        $period = "AND WEEKOFYEAR(d.data)=$w";
    } else if($_GET['period'] === 'luni'){
        $period = "AND MONTH(d.data)='".$_GET['luna']."'";
    } else if($_GET['period'] === 'perioada'){
        $from = $_GET['from'];
        $to = $_GET['to'];
        $period = "AND d.data BETWEEN '$from' AND '$to'";
    }
	$sql = "SELECT d.id, d.denum, c.task AS cild, d.cild AS idcild, d.data, d.suma, s.task AS subpro, d.subpro AS idsubpro, t.it AS tip FROM date AS d, cild AS c, subpro AS s, tip AS t WHERE d.cild=c.id AND d.subpro=s.id $tip AND d.tip=t.id $period AND d.user='".$_GET['UserID']."' ORDER by d.data DESC";
	$rs = mysql_query($sql);
	$grid = array();
	while($obj = mysql_fetch_object($rs)) {
		$grid[] = $obj;
	}
	$arr = array(
		'rows'  => $grid,
		$sql
	);
}

mysql_close($con);
echo json_encode($arr);

?>